package com.arsn.myf.flow.service.impl;

import com.arsn.myf.flow.entity.FlowProcdef;
import com.arsn.myf.flow.mapper.FlowDefinitionMapper;
import com.arsn.myf.flow.service.FlowDefinitionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 流程定义服务实现层
 * @author zhongrj
 * @since 2022-11-02
 */
@Service
public class FlowDefinitionServiceImpl extends ServiceImpl<FlowDefinitionMapper, FlowProcdef> implements FlowDefinitionService {

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 分页
     * @param flow flow 实体
     * @param page page 对象
     */
    @Override
    public IPage<FlowProcdef> selectPageFlowProcdefList(Page<FlowProcdef> page, FlowProcdef flow) {
        return page.setRecords(baseMapper.selectFlowProcdefPage(page,flow));
    }

    /**
     * 获取流程图
     * @param id 流程定义id
     */
    @Override
    public Map<String, Object> getDiagram(String id) {
        Map<String, Object> map = new HashMap<>(1);
        //获取 bpmnModel
        BpmnModel bpmnModel = repositoryService.getBpmnModel(id);
        // 流程图展示
        map.put("xml", new String(new BpmnXMLConverter().convertToXML(bpmnModel)));
        //返回
        return map;
    }
}
